package com.drink.yan.svc.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.drink.yan.core.mapper.YanMockExamsMapper;
import com.drink.yan.core.mapper.YanTaskDealMapper;
import com.drink.yan.core.mapper.YanTaskMapper;
import com.drink.yan.intf.define.*;
import com.drink.yan.intf.entity.YanCommission;
import com.drink.yan.intf.entity.YanMockExams;
import com.drink.yan.intf.entity.YanTask;
import com.drink.yan.intf.entity.YanTaskDeal;
import com.drink.yan.intf.vo.CustomerTaskSummaryVo;
import com.drink.yan.intf.vo.MockExamsVo;
import com.drink.yan.intf.vo.TaskVo;
import com.drink.yan.intf.vo.YanTaskDealVo;
import com.drink.yan.web.rest.req.cmd.ApproveTaskCmd;
import com.drink.yan.web.rest.req.cmd.CancelTaskCmd;
import com.drink.yan.web.rest.req.cmd.SaveCustomerTaskCmd;
import com.drink.yan.web.rest.req.qry.PageMockExamsQry;
import com.drink.yan.web.rest.req.qry.PageTaskQry;
import com.meta.act.app.service.ISysDictTypeService;
import com.meta.framework.act.entity.SysUser;
import com.meta.framework.common.exception.CustomException;
import com.meta.framework.common.utils.SecurityUtils;
import com.meta.framework.common.utils.StringUtils;
import com.meta.framework.core.CommonRes;
import com.meta.framework.define.DelFlagEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * java类简单作用描述
 *
 * @Title:
 * @Package: com.drink.yan.svc.service.
 * @Author: M.simple
 * @Remark: The modified content
 * @CreateDate: 2023/11/24 20:44
 * @Version: v2.0
 */
@Service
public class YanMockExamsService extends ServiceImpl<YanMockExamsMapper, YanMockExams> {

	@Resource
	private YanMockExamsMapper yanMockExamsMapper;


	public String batchSave(List<YanMockExams> yanMockExams, boolean updateSupport) {
		if (StringUtils.isNull(yanMockExams) || yanMockExams.size() == 0) {
			throw new CustomException("导入用户数据不能为空！");
		}
		int successNum = 0;
		int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
		for (YanMockExams exams : yanMockExams) {
			try {

				exams.setTotalScore(Optional.ofNullable(exams.getPolitics()).orElse(BigDecimal.ZERO)
						.add(Optional.ofNullable(exams.getEnglish()).orElse(BigDecimal.ZERO))
						.add(Optional.ofNullable(exams.getSpecialOne()).orElse(BigDecimal.ZERO))
						.add(Optional.ofNullable(exams.getSpecialTwo()).orElse(BigDecimal.ZERO)));

				LambdaQueryWrapper<YanMockExams> queryWrapper = new LambdaQueryWrapper<>();
				queryWrapper.eq(YanMockExams::getUserId, exams.getUserId());
				queryWrapper.eq(YanMockExams::getMockBatchNo, exams.getMockBatchNo());
				queryWrapper.eq(YanMockExams::getYear, exams.getYear());
				queryWrapper.eq(YanMockExams::getDelFlag, DelFlagEnum.O.getCode());
				YanMockExams u = yanMockExamsMapper.selectOne(queryWrapper);

				if (StringUtils.isNull(u)) {
					exams.setCreateBy(SecurityUtils.getUserId());
					this.save(exams);
					successNum++;
					successMsg.append("<br/>" + successNum + "、账号 " + exams.getUserName() + " 导入成功");
				} else if (updateSupport) {
					Optional.ofNullable(exams.getMockBatchNo()).ifPresent(u::setMockBatchNo);
					Optional.ofNullable(exams.getYear()).ifPresent(u::setYear);
					Optional.ofNullable(exams.getPolitics()).ifPresent(u::setPolitics);
					Optional.ofNullable(exams.getEnglish()).ifPresent(u::setEnglish);
					Optional.ofNullable(exams.getSpecialOne()).ifPresent(u::setSpecialOne);
					Optional.ofNullable(exams.getSpecialTwo()).ifPresent(u::setSpecialTwo);
					Optional.ofNullable(exams.getSpecialTwoChose()).ifPresent(u::setSpecialTwoChose);
					Optional.ofNullable(exams.getSpecialTwoJudge()).ifPresent(u::setSpecialTwoJudge);

					this.updateById(u);
					successNum++;
					successMsg.append("<br/>" + successNum + "、账号 " + exams.getUserName() + " 更新成功");
				} else {
					failureNum++;
					failureMsg.append("<br/>" + failureNum + "、账号 " + exams.getUserName() + " 已存在");
				}
			} catch (Exception e) {
				failureNum++;
				String msg = "<br/>" + failureNum + "、账号 " + exams.getUserName() + " 导入失败：";
				failureMsg.append(msg + e.getMessage());
				log.error(msg, e);
			}
		}
		if (failureNum > 0) {
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new CustomException(failureMsg.toString());
		} else {
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
		}
		return successMsg.toString();
	}

	public YanMockExams saveMockExams(YanMockExams yanMockExams) {

		yanMockExams.setTotalScore(Optional.ofNullable(yanMockExams.getPolitics()).orElse(BigDecimal.ZERO)
				.add(Optional.ofNullable(yanMockExams.getEnglish()).orElse(BigDecimal.ZERO))
				.add(Optional.ofNullable(yanMockExams.getSpecialOne()).orElse(BigDecimal.ZERO))
				.add(Optional.ofNullable(yanMockExams.getSpecialTwo()).orElse(BigDecimal.ZERO)));

		if(Objects.isNull(yanMockExams.getId())){
			this.save(yanMockExams);
		}else{
			YanMockExams u = this.getById(yanMockExams.getId());
			Optional.ofNullable(yanMockExams.getMockBatchNo()).ifPresent(u::setMockBatchNo);
			Optional.ofNullable(yanMockExams.getYear()).ifPresent(u::setYear);
			Optional.ofNullable(yanMockExams.getPolitics()).ifPresent(u::setPolitics);
			Optional.ofNullable(yanMockExams.getEnglish()).ifPresent(u::setEnglish);
			Optional.ofNullable(yanMockExams.getSpecialOne()).ifPresent(u::setSpecialOne);
			Optional.ofNullable(yanMockExams.getSpecialTwo()).ifPresent(u::setSpecialTwo);
			Optional.ofNullable(yanMockExams.getSpecialTwoChose()).ifPresent(u::setSpecialTwoChose);
			Optional.ofNullable(yanMockExams.getSpecialTwoJudge()).ifPresent(u::setSpecialTwoJudge);

			this.updateById(u);
			yanMockExams = u;
		}

		return yanMockExams;
	}

	public List<MockExamsVo> listMockExams(PageMockExamsQry qry) {

		List<MockExamsVo> mockExamsVos = yanMockExamsMapper.listMockExams(qry);
		/*mockExamsVos.forEach(x->{
			x.setTotalScore(Optional.ofNullable(x.getPolitics()).orElse(BigDecimal.ZERO)
					.add(Optional.ofNullable(x.getEnglish()).orElse(BigDecimal.ZERO))
					.add(Optional.ofNullable(x.getSpecialOne()).orElse(BigDecimal.ZERO))
					.add(Optional.ofNullable(x.getSpecialTwo()).orElse(BigDecimal.ZERO)));
		});*/
		return mockExamsVos;
	}

	public Long maxYear() {
		return yanMockExamsMapper.maxYear();
	}
}
